package com.ihome.excel.xlsx.engine.imports;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.ihome.excel.xlsx.entity.EncapsulationEnum;
import com.ihome.excel.xlsx.entity.TemplateConfig;
import com.ihome.excel.xlsx.exceptions.RowsBusinessException;
import com.ihome.excel.xlsx.exceptions.RowsValidateException;
import com.ihome.excel.xlsx.exceptions.ThrowableDefine;
import com.ihome.excel.xlsx.validate.ICellsValidateRule;
import com.ihome.excel.xlsx.validate.IRowsValidateRule;
import com.ihome.excel.xlsx.validate.impl.DefaultValidateRule;

@SuppressWarnings("all")
public class SheetParseEngine {

    private RowParseEngine rowlParse = new RowParseEngine();

    public List getParserList(Workbook workBook, TemplateConfig config, ICellsValidateRule validateRule, IRowsValidateRule rowValidataRule) {
        if (workBook == null) {
            throw new IllegalArgumentException("workBook不能为空");
        }
        if (config == null) {
            throw new IllegalArgumentException("config参数不能为空");
        }
        Class classes = null;
        if (EncapsulationEnum.ENTITY.getCode() == config.getEncapEnum().getCode()) {
            try {
                classes = Class.forName(config.getClassa());
            } catch (ClassNotFoundException e) {
                throw ThrowableDefine.MAPING_ENTITY_PATH_ERROR.getTemplateParseExp(e.getMessage());
            }
        }
        if (validateRule == null) {
            validateRule = config.getDefaultValidateRule();
        }
        Sheet sheet = workBook.getSheetAt(0);
        Iterator<Row> rowIterator = sheet.rowIterator();
        /**
         * 不读列头
         */
        if (rowIterator.hasNext()) {
            rowIterator.next();
        }
        List resultList = new ArrayList<>();
        Object rowValue = null;
        int rowsIteratorCount = 0;
        List<Object> validateList = new ArrayList<Object>();
        while (rowIterator.hasNext()) {
            ++rowsIteratorCount;
            /**
             * 如果映射类型是Map弱类型
             */
            if (EncapsulationEnum.MAP.getCode() == config.getEncapEnum().getCode()) {
                rowValue = rowlParse.getRowValueMap(rowIterator.next(), config, validateRule);
            } else {
                rowValue = rowlParse.getRowValueEntity(rowIterator.next(), classes, config, validateRule);
            }
            if (rowValidataRule != null) {
                try {
                    if (rowValidataRule.rowsBatch() > 1) {
                        validateList.add(rowValue);
                        if (rowsIteratorCount == rowValidataRule.rowsBatch()) {
                            rowValidataRule.rowValidateRule(validateList);
                            rowsIteratorCount = 0;
                            validateList = new ArrayList<>();
                        }
                    } else {
                        rowValidataRule.rowValidateRule(rowValue);
                    }
                } catch (RowsBusinessException e) {
                    throw new RowsValidateException(e.getMessage());
                }
            }
            resultList.add(rowValue);
        }
        return resultList;
    }
}
